<?php
/**
 * 费用
 */
declare (strict_types = 1);
namespace app\sk\service;

use app\core\Relation;
use app\core\Res;
use app\core\ExcelService;
use think\facade\Db;

class ActionService
{

    /**
     * 费用列表
     * @param $params
     * @return $data
     */
    public static function indexView(array $params,$export=0){
        $action=Res::kv($params,'mtz');
        if($action==''){
            return '路径错误';
        }
        $action_model= Relation::getModel('table_action');
        $aData=Res::findData($action_model,['action_name'=>$action]);
        if(!$aData){
            return '路径错误';
        }
        if($aData['action_type']==1){
            ActionType::indexView($aData);
        }elseif($aData['action_type']==2){
            ActionType::addView($aData);
        }elseif($aData['action_type']==3){
            ActionType::editView($aData);
        }

    }


    /**
     * 查询列表
     * @param $params
     * @return $data
     */
    public static function list(array $params,$export=0){
        $actionid=Res::kv($params,'actionid');
        $aData=Res::allTableData('table_action_field',['action_id'=>$actionid]);
        $tablename='';
        $keylike=[];
        $likestr='';
        $queryField=[];
        foreach($aData as $aitems){
            if($tablename==''){
                $tablename=$aitems['table_name'];
            }
            if($aitems['field_type_id']>5 && $aitems['field_type_id']<11){
                $keylike[]=$aitems['field_name'];
            }

            if($aitems['field_show']==1){
                $queryField[$aitems['field_name']]='=';
            }
        }
        if($tablename==''){
            return [];
        }
        if($keylike){
            $likestr=implode('|',$keylike);
        }
        $model= Relation::getModel($tablename);
        
        $map=[];

        $createDateSel=Res::kv($params,'createDateSel');
        if($createDateSel!=''){
            $dtlist=explode(' - ', $createDateSel);
            $start_time=Res::kv($dtlist,0);
            $end_time=Res::kv($dtlist,1);
            $map[] = ['cdate','>=',$start_time];
            $map[] = ['cdate','<=',$end_time];
        }

        

        $map=Res::querymap($model,$map,$queryField,$params);

        $keywords=Res::kv($params,'keywords');
        if($keywords!='' && $likestr!=''){
            $map[] = [$likestr,'like',"%".$keywords."%"];
        }

        $model=$model->where($map);


        //导出数据
        if($export==1){
            $list=$model->select();
            $tmp=[];
            foreach($list as $key=>$v){
                $tmp[$key]=$v;
            }
            return $tmp;
        }
        //分页数据显示
        $data= Res::page($model,$params);

        $tmp=[];
        foreach($data['data'] as $key=>$v){
            $tmp[$key]=$v;
        }
        $data['data']=$tmp;

        return $data;
    }

    /**
     * 添加
     * @param $params
     * @return $data
     */
    public static function add(array $params){
        $redata=[];
        $msg='';

        $actionid=Res::kv($params,'actionid');
        $aData=Res::allTableData('table_action_field',['action_id'=>$actionid]);
        $tablename='';
        foreach($aData as $aitems){
            if($tablename==''){
                $tablename=$aitems['table_name'];
            }
        }
        if($tablename==''){
            $redata=['code'=>0,'msg'=>'字典不存在','data'=>[]];
            return $redata;
        }

        
        $model=Relation::getModel($tablename);

        if($msg!=''){
            $redata=['code'=>0,'msg'=>$msg,'data'=>[]];
            return $redata;
        }
        $saveData=$params;
        
        Db::startTrans();
        try {
           
            $res=Res::saveModelData($model,$saveData,['id'=>'add']);
            if($res>0){
                Db::commit();
                return  ['code'=>1,'msg'=>'数据保存成功','data'=>[]];
            }else{
                Db::rollback();
                return  ['code'=>0,'msg'=>'数据保存失败,请查看项目是否输入完整','data'=>[]];
            }
        }catch (\PDOException $e){
            Db::rollback();
            return  ['code'=>0,'msg'=>'数据保存失败','data'=>[]];
        }
    }

    /**
     * 编辑
     * @param $params
     * @return $data
     */
    public static function edit(array $params){
        $redata=[];
        $msg='';

        $actionid=Res::kv($params,'actionid');
        $id=Res::kv($params,'id',0);
        $aData=Res::allTableData('table_action_field',['action_id'=>$actionid]);
        $tablename='';
        foreach($aData as $aitems){
            if($tablename==''){
                $tablename=$aitems['table_name'];
            }
        }
        if($tablename==''){
            $redata=['code'=>0,'msg'=>'字典不存在','data'=>[]];
            return $redata;
        }

        
        $model=Relation::getModel($tablename);

        if($msg!='' && $id<1){
            $redata=['code'=>0,'msg'=>$msg,'data'=>[]];
            return $redata;
        }
        $saveData=$params;
        
        Db::startTrans();
        try {
           
            $res=Res::saveModelData($model,$saveData,['id'=>$id]);
            if($res>0){
                Db::commit();
                return  ['code'=>1,'msg'=>'数据保存成功','data'=>[]];
            }else{
                Db::rollback();
                return  ['code'=>0,'msg'=>'数据保存失败,请查看项目是否输入完整','data'=>[]];
            }
        }catch (\PDOException $e){
            Db::rollback();
            return  ['code'=>0,'msg'=>'数据保存失败','data'=>[]];
        }
    }

    /**
     * 删除
     * @param $params
     * @return $data
     */
    public static function del(array $params){
        $actionid=Res::kv($params,'actionid');
        $id=Res::kv($params,'id',0);
        $aData=Res::allTableData('table_action_field',['action_id'=>$actionid]);
        $tablename='';
        foreach($aData as $aitems){
            if($tablename==''){
                $tablename=$aitems['table_name'];
            }
        }
        if($tablename==''){
            return[];
        }

        
        $model=Relation::getModel($tablename);
        $map[] = ['id','=',$id];
        $map=Res::querymap($model,$map);
        $model->where($map)->delete();
        return [];
    }

    /**
     * 导出列表
     * @param $params
     * @return $data
     */
    public static function export(array $params,$export=0){
        $actionid=Res::kv($params,'actionid');
        $aData=Res::allTableData('table_action_field',['action_id'=>$actionid]);
        $tablename='';
        $table_name_zh='';
        $keylike=[];
        $likestr='';
        $queryField=[];

        $header=[];
        foreach($aData as $aitems){
            if($tablename==''){
                $tablename=$aitems['table_name'];
            }
            if($table_name_zh==''){
                $table_name_zh=$aitems['table_name_zh'];
            }
            if($aitems['field_type_id']>5 && $aitems['field_type_id']<11){
                $keylike[]=$aitems['field_name'];
            }

            if($aitems['field_show']==1){
                $queryField[$aitems['field_name']]='=';
            }

            if($aitems['field_export']==1){
                $header[]=[$aitems['field_name'],$aitems['field_name_zh']];
            }
        }
        if($tablename==''){
            return [];
        }
        if($keylike){
            $likestr=implode('|',$keylike);
        }
        $model= Relation::getModel($tablename);
        
        $map=[];

        $createDateSel=Res::kv($params,'createDateSel');
        if($createDateSel!=''){
            $dtlist=explode(' - ', $createDateSel);
            $start_time=Res::kv($dtlist,0);
            $end_time=Res::kv($dtlist,1);
            $map[] = ['cdate','>=',$start_time];
            $map[] = ['cdate','<=',$end_time];
        }

        

        $map=Res::querymap($model,$map,$queryField,$params);

        $keywords=Res::kv($params,'keywords');
        if($keywords!='' && $likestr!=''){
            $map[] = [$likestr,'like',"%".$keywords."%"];
        }

        $list=$model->where($map)->select();
        
        $tmp=[];
        foreach($list as $key=>$v){
            $tmp[$key]=$v;
        }

        $fileName = $table_name_zh . '导出日期' . date('Y-m-d H:i:s', time());
        ExcelService::export($header,$tmp,$fileName,['titleName'=>$table_name_zh]);
        
    }

    
}